package com.eat.fish_service.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eat.common_utils.R;
import com.eat.fish_service.entity.Dao.SensorData;
import com.eat.fish_service.mapper.SensorDataMapper;
import com.eat.fish_service.service.SensorDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author CZF
 * @since 2022-04-11
 */
@Service
public class SensorDataServiceImpl extends ServiceImpl<SensorDataMapper, SensorData> implements SensorDataService {

    @Autowired
    RestTemplate restTemplate;

    @Override
    public R getPh(Long aquariumId, Long time) {
//        try {
//            Thread.sleep(1000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date(time);

        //这一天的开头
        DateTime beginOfToDay = DateUtil.beginOfDay(date);
        //这一天的结尾
        DateTime beginOfTomorrow = DateUtil.offsetDay(beginOfToDay, 1);
        Date beginOfToDay1 = beginOfToDay.toJdkDate();
        Date beginOfTomorrow1 = beginOfTomorrow.toJdkDate();

        //今天的开始
        DateTime beginOfRealToday = DateUtil.beginOfDay(new Date());

        //如果要获取的不是今天的数据,返回那一天的所有数据
        if(beginOfRealToday.isAfter(date)){
            //查询这一天所有ph数据
            QueryWrapper<SensorData> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("value1").ge("gmt_create",beginOfToDay1).lt("gmt_create",beginOfTomorrow1).orderByAsc("gmt_create");
            List<SensorData> sensorDataList = baseMapper.selectList(queryWrapper);
            List<String> phList = new ArrayList<>();
            for (SensorData sensorDate:sensorDataList) {
                phList.add(sensorDate.getValue1());
            }

            //查询这一天所有时间点
            QueryWrapper<SensorData> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.select("gmt_create").ge("gmt_create",beginOfToDay1).lt("gmt_create",beginOfTomorrow1).orderByAsc("gmt_create");
            List<SensorData> sensorDataList1 = baseMapper.selectList(queryWrapper1);
            List<String> timeList = new ArrayList<>();
            for(SensorData sensorData:sensorDataList1){
                timeList.add(sensorData.getGmtCreate().getHours()+":"+sensorData.getGmtCreate().getMinutes());
            }

            return R.ok().data("phList",phList).data("timeList",timeList);

        }else{
            //查询这一天所有ph数据
            QueryWrapper<SensorData> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("value1").ge("gmt_create",beginOfToDay1).lt("gmt_create",beginOfTomorrow1).orderByAsc("gmt_create");
            List<SensorData> sensorDataList = baseMapper.selectList(queryWrapper);
            List<String> phList = new ArrayList<>();
            for (SensorData sensorDate:sensorDataList) {
                phList.add(sensorDate.getValue1());
            }

            //查询这一天所有时间点
            QueryWrapper<SensorData> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.select("gmt_create").ge("gmt_create",beginOfToDay1).lt("gmt_create",beginOfTomorrow1).orderByAsc("gmt_create");
            List<SensorData> sensorDataList1 = baseMapper.selectList(queryWrapper1);
            List<String> timeList = new ArrayList<>();
            for(SensorData sensorData:sensorDataList1){
                timeList.add(sensorData.getGmtCreate().getHours()+":"+sensorData.getGmtCreate().getMinutes());
            }

            //todo: 发送今天的数据给预测算法接口，返回预测的结果,再封装结果返回给前端
            String url="http://localhost:xxx";
            //todo：这个返回的Object需要自己写一个对象来接收响应对象
            Object forObject = restTemplate.getForObject(url, Object.class);

            //todo：封装返回结果


            return R.ok().data("phList",phList).data("timeList",timeList);
        }

    }

    @Override
    public R getTem(Long aquariumId, Long time) {
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date(time);

        //这一天的开头
        DateTime beginOfToDay = DateUtil.beginOfDay(date);
        //这一天的结尾
        DateTime beginOfTomorrow = DateUtil.offsetDay(beginOfToDay, 1);
        Date beginOfToDay1 = beginOfToDay.toJdkDate();
        Date beginOfTomorrow1 = beginOfTomorrow.toJdkDate();

        //今天的开始
        DateTime beginOfRealToday = DateUtil.beginOfDay(new Date());

        //如果要获取的不是今天的数据,返回那一天的所有数据
        if(beginOfRealToday.isAfter(date)){
            //查询这一天所有ph数据
            QueryWrapper<SensorData> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("value2").ge("gmt_create",beginOfToDay1).lt("gmt_create",beginOfTomorrow1).orderByAsc("gmt_create");
            List<SensorData> sensorDataList = baseMapper.selectList(queryWrapper);
            List<String> temList = new ArrayList<>();
            for (SensorData sensorDate:sensorDataList) {
                temList.add(sensorDate.getValue2());
            }

            //查询这一天所有时间点
            QueryWrapper<SensorData> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.select("gmt_create").ge("gmt_create",beginOfToDay1).lt("gmt_create",beginOfTomorrow1).orderByAsc("gmt_create");
            List<SensorData> sensorDataList1 = baseMapper.selectList(queryWrapper1);
            List<String> timeList = new ArrayList<>();
            for(SensorData sensorData:sensorDataList1){
                timeList.add(sensorData.getGmtCreate().getHours()+":"+sensorData.getGmtCreate().getMinutes());
            }

            return R.ok().data("temList",temList).data("timeList",timeList);

        }else{
            //查询这一天所有ph数据
            QueryWrapper<SensorData> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("value2").ge("gmt_create",beginOfToDay1).lt("gmt_create",beginOfTomorrow1).orderByAsc("gmt_create");
            List<SensorData> sensorDataList = baseMapper.selectList(queryWrapper);
            List<String> temList = new ArrayList<>();
            for (SensorData sensorDate:sensorDataList) {
                temList.add(sensorDate.getValue2());
            }

            //查询这一天所有时间点
            QueryWrapper<SensorData> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.select("gmt_create").ge("gmt_create",beginOfToDay1).lt("gmt_create",beginOfTomorrow1).orderByAsc("gmt_create");
            List<SensorData> sensorDataList1 = baseMapper.selectList(queryWrapper1);
            List<String> timeList = new ArrayList<>();
            for(SensorData sensorData:sensorDataList1){
                timeList.add(sensorData.getGmtCreate().getHours()+":"+sensorData.getGmtCreate().getMinutes());
            }

            //todo: 发送今天的数据给预测算法接口，返回预测的结果,再封装结果返回给前端
            String url="http://localhost:xxx";
            //todo：这个返回的Object需要自己写一个对象来接收响应对象
            Object forObject = restTemplate.getForObject(url, Object.class);

            //todo：封装返回结果

            return R.ok().data("temList",temList).data("timeList",timeList);
        }
    }

    @Override
    public R getDianDao(Long aquariumId, Long time) {
        Date date=new Date(time);

        //这一天的开头
        DateTime beginOfToDay = DateUtil.beginOfDay(date);
        //这一天的结尾
        DateTime beginOfTomorrow = DateUtil.offsetDay(beginOfToDay, 1);
        Date beginOfToDay1 = beginOfToDay.toJdkDate();
        Date beginOfTomorrow1 = beginOfTomorrow.toJdkDate();

        //今天的开始
        DateTime beginOfRealToday = DateUtil.beginOfDay(new Date());

        //如果要获取的不是今天的数据,返回那一天的所有数据
        if(beginOfRealToday.isAfter(date)){
            //查询这一天所有ph数据
            QueryWrapper<SensorData> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("value3").ge("gmt_create",beginOfToDay1).lt("gmt_create",beginOfTomorrow1).orderByAsc("gmt_create");
            List<SensorData> sensorDataList = baseMapper.selectList(queryWrapper);
            List<String> dianDaoList = new ArrayList<>();
            for (SensorData sensorDate:sensorDataList) {
                dianDaoList.add(sensorDate.getValue3());
            }

            //查询这一天所有时间点
            QueryWrapper<SensorData> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.select("gmt_create").ge("gmt_create",beginOfToDay1).lt("gmt_create",beginOfTomorrow1).orderByAsc("gmt_create");
            List<SensorData> sensorDataList1 = baseMapper.selectList(queryWrapper1);
            List<String> timeList = new ArrayList<>();
            for(SensorData sensorData:sensorDataList1){
                timeList.add(sensorData.getGmtCreate().getHours()+":"+sensorData.getGmtCreate().getMinutes());
            }

            return R.ok().data("dianDaoList",dianDaoList).data("timeList",timeList);

        }else{
            //查询这一天所有ph数据
            QueryWrapper<SensorData> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("value3").ge("gmt_create",beginOfToDay1).lt("gmt_create",beginOfTomorrow1).orderByAsc("gmt_create");
            List<SensorData> sensorDataList = baseMapper.selectList(queryWrapper);
            List<String> dianDaoList = new ArrayList<>();
            for (SensorData sensorDate:sensorDataList) {
                dianDaoList.add(sensorDate.getValue3());
            }

            //查询这一天所有时间点
            QueryWrapper<SensorData> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.select("gmt_create").ge("gmt_create",beginOfToDay1).lt("gmt_create",beginOfTomorrow1).orderByAsc("gmt_create");
            List<SensorData> sensorDataList1 = baseMapper.selectList(queryWrapper1);
            List<String> timeList = new ArrayList<>();
            for(SensorData sensorData:sensorDataList1){
                timeList.add(sensorData.getGmtCreate().getHours()+":"+sensorData.getGmtCreate().getMinutes());
            }

            //todo: 发送今天的数据给预测算法接口，返回预测的结果,再封装结果返回给前端
            String url="http://localhost:xxx";
            //todo：这个返回的Object需要自己写一个对象来接收响应对象
            Object forObject = restTemplate.getForObject(url, Object.class);

            //todo：封装返回结果

            return R.ok().data("dianDaoList",dianDaoList).data("timeList",timeList);
        }
    }

    @Override
    public R getHunZhuo(Long aquariumId, Long time) {
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date(time);

        //这一天的开头
        DateTime beginOfToDay = DateUtil.beginOfDay(date);
        //这一天的结尾
        DateTime beginOfTomorrow = DateUtil.offsetDay(beginOfToDay, 1);
        Date beginOfToDay1 = beginOfToDay.toJdkDate();
        Date beginOfTomorrow1 = beginOfTomorrow.toJdkDate();

        //今天的开始
        DateTime beginOfRealToday = DateUtil.beginOfDay(new Date());

        //如果要获取的不是今天的数据,返回那一天的所有数据
        if(beginOfRealToday.isAfter(date)){
            //查询这一天所有ph数据
            QueryWrapper<SensorData> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("value4").ge("gmt_create",beginOfToDay1).lt("gmt_create",beginOfTomorrow1).orderByAsc("gmt_create");
            List<SensorData> sensorDataList = baseMapper.selectList(queryWrapper);
            List<String> hunZhuoList = new ArrayList<>();
            for (SensorData sensorDate:sensorDataList) {
                hunZhuoList.add(sensorDate.getValue4());
            }

            //查询这一天所有时间点
            QueryWrapper<SensorData> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.select("gmt_create").ge("gmt_create",beginOfToDay1).lt("gmt_create",beginOfTomorrow1).orderByAsc("gmt_create");
            List<SensorData> sensorDataList1 = baseMapper.selectList(queryWrapper1);
            List<String> timeList = new ArrayList<>();
            for(SensorData sensorData:sensorDataList1){
                timeList.add(sensorData.getGmtCreate().getHours()+":"+sensorData.getGmtCreate().getMinutes());
            }

            return R.ok().data("hunZhuoList",hunZhuoList).data("timeList",timeList);

        }else{
            //查询这一天所有ph数据
            QueryWrapper<SensorData> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("value4").ge("gmt_create",beginOfToDay1).lt("gmt_create",beginOfTomorrow1).orderByAsc("gmt_create");
            List<SensorData> sensorDataList = baseMapper.selectList(queryWrapper);
            List<String> hunZhuoList = new ArrayList<>();
            for (SensorData sensorDate:sensorDataList) {
                hunZhuoList.add(sensorDate.getValue4());
            }

            //查询这一天所有时间点
            QueryWrapper<SensorData> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.select("gmt_create").ge("gmt_create",beginOfToDay1).lt("gmt_create",beginOfTomorrow1).orderByAsc("gmt_create");
            List<SensorData> sensorDataList1 = baseMapper.selectList(queryWrapper1);
            List<String> timeList = new ArrayList<>();
            for(SensorData sensorData:sensorDataList1){
                timeList.add(sensorData.getGmtCreate().getHours()+":"+sensorData.getGmtCreate().getMinutes());
            }

            //todo: 发送今天的数据给预测算法接口，返回预测的结果,再封装结果返回给前端
            String url="http://localhost:xxx";
            //todo：这个返回的Object需要自己写一个对象来接收响应对象
            Object forObject = restTemplate.getForObject(url, Object.class);

            //todo：封装返回结果

            return R.ok().data("hunZhuoList",hunZhuoList).data("timeList",timeList);
        }
    }

    @Override
    public R getRongJieYang(Long aquariumId, Long time) {
        //        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date(time);

        //这一天的开头
        DateTime beginOfToDay = DateUtil.beginOfDay(date);
        //这一天的结尾
        DateTime beginOfTomorrow = DateUtil.offsetDay(beginOfToDay, 1);
        Date beginOfToDay1 = beginOfToDay.toJdkDate();
        Date beginOfTomorrow1 = beginOfTomorrow.toJdkDate();

        //今天的开始
        DateTime beginOfRealToday = DateUtil.beginOfDay(new Date());

        //如果要获取的不是今天的数据,返回那一天的所有数据
        if(beginOfRealToday.isAfter(date)){
            //查询这一天所有ph数据
            QueryWrapper<SensorData> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("value5").ge("gmt_create",beginOfToDay1).lt("gmt_create",beginOfTomorrow1).orderByAsc("gmt_create");
            List<SensorData> sensorDataList = baseMapper.selectList(queryWrapper);
            List<String> rongJieYangList = new ArrayList<>();
            for (SensorData sensorDate:sensorDataList) {
                rongJieYangList.add(sensorDate.getValue5());
            }

            //查询这一天所有时间点
            QueryWrapper<SensorData> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.select("gmt_create").ge("gmt_create",beginOfToDay1).lt("gmt_create",beginOfTomorrow1).orderByAsc("gmt_create");
            List<SensorData> sensorDataList1 = baseMapper.selectList(queryWrapper1);
            List<String> timeList = new ArrayList<>();
            for(SensorData sensorData:sensorDataList1){
                timeList.add(sensorData.getGmtCreate().getHours()+":"+sensorData.getGmtCreate().getMinutes());
            }

            return R.ok().data("rongJieYangList",rongJieYangList).data("timeList",timeList);

        }else{
            //查询这一天所有ph数据
            QueryWrapper<SensorData> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("value5").ge("gmt_create",beginOfToDay1).lt("gmt_create",beginOfTomorrow1).orderByAsc("gmt_create");
            List<SensorData> sensorDataList = baseMapper.selectList(queryWrapper);
            List<String> rongJieYangList = new ArrayList<>();
            for (SensorData sensorDate:sensorDataList) {
                rongJieYangList.add(sensorDate.getValue5());
            }

            //查询这一天所有时间点
            QueryWrapper<SensorData> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.select("gmt_create").ge("gmt_create",beginOfToDay1).lt("gmt_create",beginOfTomorrow1).orderByAsc("gmt_create");
            List<SensorData> sensorDataList1 = baseMapper.selectList(queryWrapper1);
            List<String> timeList = new ArrayList<>();
            for(SensorData sensorData:sensorDataList1){
                timeList.add(sensorData.getGmtCreate().getHours()+":"+sensorData.getGmtCreate().getMinutes());
            }

            //todo: 发送今天的数据给预测算法接口，返回预测的结果,再封装结果返回给前端
            String url="http://localhost:xxx";
            //todo：这个返回的Object需要自己写一个对象来接收响应对象
            Object forObject = restTemplate.getForObject(url, Object.class);

            //todo：封装返回结果

            return R.ok().data("rongJieYangList",rongJieYangList).data("timeList",timeList);
        }
    }
}
